package com.srwl.mytx.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.domain.AliOssService;
import com.srwl.mytx.domain.MyFile;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.widget.CheckView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyFileRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "DonationRecordAdapter";
    private static final int ITEM_TYPE_NO_DATA = -1;
    private List<MyFile> myFileList;
    private Context context;
    private InteractionListener listener;
    private int checkCount;
    private boolean enableSelectPicture = true;
    private boolean enableSelectVideo = true;

    public MyFileRecordAdapter(Context context, List<MyFile> myFileList, InteractionListener listener) {
        this.myFileList = myFileList;
        this.context = context;
        this.listener = listener;

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ITEM_TYPE_NO_DATA:
                return new NoDataViewHolder(LayoutInflater.from(context).inflate(R.layout.no__data__layout, parent, false));
            default:
                return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.my_file_record_item_layout, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (myFileList.size() == 0) {
           NoDataViewHolder viewHolder= (NoDataViewHolder) holder;
           viewHolder.tv_tips.setText("仓库是空的");
            return;
        }
        MyViewHolder viewHolder = (MyViewHolder) holder;
        viewHolder.itemView.setFocusable(true);
        MyFile myFile = myFileList.get(position);
        //如果选择的数量已经等于设置的选择最大数量了，还没有被选中的item,设置为不可用状态
        if (myFile.getType() == Constant.MY_FILE_TYPE_PICTURE) {
            viewHolder.iv_playVideo.setVisibility(View.INVISIBLE);
            if (myFile.getOrderNum() == null) {

                viewHolder.checkView.removeCount();
                if (enableSelectPicture) {
                    viewHolder.checkView.setEnableCheck(true);
                    setClickLinstener(position, viewHolder);
                } else {
                    viewHolder.checkView.setEnableCheck(false);
                    viewHolder.rl_check.setOnClickListener(null);//不设置为空的话，在view 的复用中，可能出现原本没设置点击监听的item，都会触发点击监听
                }

            } else {
                viewHolder.checkView.setCount(myFile.getOrderNum());
                setClickLinstener(position, viewHolder);
            }

//            if (!enableSelectPicture && myFile.getOrderNum() ==null) {
//                viewHolder.checkView.setEnabled(false);
//                viewHolder.checkView.setOnClickListener(null);
//            } else {
//                if (myFile.getOrderNum() != null) {
//                    viewHolder.checkView.setCount(myFile.getOrderNum());
//                } else {
//                    viewHolder.checkView.removeCount();
//                }
//                setClickLinstener(position, viewHolder);
//            }
        } else {
            viewHolder.iv_playVideo.setVisibility(View.VISIBLE);
//            if (!enableSelectVideo && myFile.getOrderNum() == Integer.MIN_VALUE) {
//                viewHolder.checkView.setEnabled(false);
//                viewHolder.checkView.setOnClickListener(null);
//            } else {
//                if (myFile.getOrderNum() != null) {
//                    viewHolder.checkView.setCount(myFile.getOrderNum());
//                } else {
//                    viewHolder.checkView.removeCount();
//                }
//                setClickLinstener(position, viewHolder);
//            }
            if (myFile.getOrderNum() == null) {
                viewHolder.checkView.removeCount();
                if (enableSelectVideo) {
                    viewHolder.checkView.setEnableCheck(true);
                    setClickLinstener(position, viewHolder);
                } else {
                    viewHolder.checkView.setEnableCheck(false);
                    viewHolder.rl_check.setOnClickListener(null);
                }
            } else {
                viewHolder.checkView.setCount(myFile.getOrderNum());
                setClickLinstener(position, viewHolder);
            }
        }
        String url= myFile.getUrl();
        String remoteUrl="";
        if (url != null && url.startsWith("http")) {
            remoteUrl = url;
        } else if (myFile.getType()==Constant.MY_FILE_TYPE_VIDEO){
            remoteUrl = AliOssService.TOPIC_VIDEO_PRE_URL + url;
        }else if (myFile.getType()==Constant.MY_FILE_TYPE_PICTURE){
            remoteUrl = AliOssService.TOPIC_IMAGE_PRE_URL + url;

        }


       // Glide.with(context).load(remoteUrl).diskCacheStrategy(DiskCacheStrategy.ALL).into(viewHolder.iv_myFile);
        GlideLoadUtils.glideLoad(context,remoteUrl,viewHolder.iv_myFile);
        Log.d(TAG, "onBindViewHolder:myFile.getOrderNum() " + myFile.getOrderNum());

    }

    private void setClickLinstener(int position, MyViewHolder viewHolder) {
        viewHolder.rl_check.setFocusable(true);
        viewHolder.rl_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (listener != null) {
                    listener.onItemClick(viewHolder.checkView, position);
                }
            }
        });
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {

        if (myFileList.size() == 0) {
            return 1;
        }
        return myFileList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (myFileList.size() == 0) {
            return ITEM_TYPE_NO_DATA;
        }
        return myFileList.get(position).getType();


    }

    public void setEnableSelectPicture(boolean enableSelectPicture) {
        this.enableSelectPicture = enableSelectPicture;
        notifyDataSetChanged();
    }

    public void setEnableSelectVideo(boolean enableSelectVideo) {
        this.enableSelectVideo = enableSelectVideo;
        notifyDataSetChanged();
    }

    class NoDataViewHolder extends RecyclerView.ViewHolder {
        TextView tv_tips;

        public NoDataViewHolder(View itemView) {
            super(itemView);
            tv_tips = itemView.findViewById(R.id.tv_tips);
        }
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.check_view)
        CheckView checkView;
        @BindView(R.id.iv_my_file)
        ImageView iv_myFile;
        @BindView(R.id.iv_play_video)
        ImageView iv_playVideo;
        @BindView(R.id.rl_check)
        RelativeLayout rl_check;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setListener(InteractionListener listener) {
        this.listener = listener;
    }

    public interface InteractionListener {
        void onItemClick(CheckView checkView, int position);
    }

}